package com.example.demoreptile;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @className: DemoTest1
 * @description:
 * @author: zhangjunfa
 * @date: 2023/7/17 15:23
 */
@Slf4j
public class DemoTest1 {

    private static String url = "https://www.tianqi.com/chinacity.html";

    public static void main(String[] args) throws IOException {
        url = URLDecoder.decode(url, "UTF-8");

        //模拟浏览器访问，防止阻止跳到登录页面
        Connection mozilla = Jsoup.connect(url).userAgent("Mozilla");
        Document document = mozilla.get();

        Element element = document.body();

        Elements citybox = element.getElementsByClass("citybox");
        List<Content> result = new ArrayList<>();
        for (int i = 0; i < citybox.size(); i++) {
            Element element1 = citybox.get(i);
            Content content = new Content();
            String h2 = element1.getElementsByTag("h2").text();
            String span = element1.getElementsByTag("span").text();
            content.setCityName(h2);
            content.setXqName(span);
            result.add(content);
        }

        log.info("结果：{}",result.toString());
    }


}
